﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class XuanZeZhanJi : MonoBehaviour {

	//向左
	Button left_Button;
	//向右
	Button right_Button;
	//开始游戏
	Button beginGame;
	RectTransform obj;
	//X坐标
	float m_x =0;
	//移动速度
	float m_speed =700;
	//是否向左移动
	bool left_bool = false;
	//是否向右移动
	bool right_bool = false;
	//条件值
	int m_a =0;
	//选择的战机的序号
	public static int xuhao_Zhanji =0;
    //战机的名称
    Image zhanji_name;
    Image zhanji_jieshao;
    public Sprite[] spr_name = new Sprite[3];
    public Sprite[] spr_jieshao = new Sprite[3];
	void Start () {
	
		left_Button = transform.Find("/Canvas/Left").GetComponent<Button>();
		EventTriggerListener.Get(left_Button.gameObject).onClick = OnLeft;
		right_Button = transform.Find("/Canvas/Right").GetComponent<Button>();
		EventTriggerListener.Get(right_Button.gameObject).onClick = OnRight;
		beginGame = transform.Find("/Canvas/Start_Game").GetComponent<Button>();
		EventTriggerListener.Get(beginGame.gameObject).onClick = Onstartgame;
		obj = GameObject.FindGameObjectWithTag("XuanZeZhanJi").gameObject.GetComponent<RectTransform>();

        zhanji_name = transform.Find("/Canvas/name/Image-name").GetComponent<Image>();
        zhanji_jieshao = transform.Find("/Canvas/name/Image-JieShao").GetComponent<Image>();
	}

	void Update () {
	
		//向左移动
		if(left_bool&&m_x>=-660)
		{
		  m_x-=Time.deltaTime*m_speed;
		  if(m_x <= m_a)
		  {
				m_x =m_a; //jiaozheng(m_x);
				left_bool = false;
		  }
		}
		//向右移动
		if(right_bool&&m_x<0)
		{
			m_x+=Time.deltaTime*m_speed;
			if(m_x >= m_a)
			{
				m_x =m_a; //jiaozheng(m_x);
				right_bool = false;
			}
		}

        this.name((int)m_x);
		obj.transform.localPosition = new Vector3(m_x,0,0);
	}

    private void name(int m_a)
    {
        switch (m_a)
        { 
            case 0:
                zhanji_name.sprite = spr_name[0];//Resources.Load<Sprite>("Texture/select_rank_5");
                zhanji_jieshao.sprite = spr_jieshao[0];
                break;
            case -330:
                zhanji_name.sprite = spr_name[1];
                zhanji_jieshao.sprite = spr_jieshao[1];
                break;
            case -660:
                zhanji_name.sprite = spr_name[2];
                zhanji_jieshao.sprite = spr_jieshao[2];
                break;
        }
    }

	private void OnLeft(GameObject go)
	{
		if(go ==left_Button.gameObject)
		{
		if(m_a<=-660)
		{
			return;
		}else{
		  m_a-=330;
		}
		left_bool = true;
		}
	}
	private void OnRight(GameObject go)
	{
		if(go ==right_Button.gameObject)
		{
		if(m_x>=0&&m_x<=330)
		{
			return;
		}else{
		m_a+=330;
		}
		right_bool = true;
		}
	}

	//矫正坐标
	private float jiaozheng(float m_f)
	{
		float m_F = 0;
		if(m_f>=-10&&m_f<=10)
		{
			m_F =0;
		}else if(m_f<=-320&&m_f>=-340){
			m_F =-330;
		}else if(m_f<=-660&&m_f>=-670){
			m_F =-660;
		}
		return m_F;
	}

	//开始游戏
	private void Onstartgame(GameObject go)
	{
		if(go ==beginGame.gameObject)
		{
		if(m_x==0)
		{
			xuhao_Zhanji =0;
		}else if(m_x==-330){
			xuhao_Zhanji =1;
		}else if(m_x==-660)
		{
			xuhao_Zhanji=2;
		}
			if(m_x==0||m_a==-330||m_a==-660)
			{
		       switch(GuanQia.level_number)
				{
				case 1:
				    LoadScene.name = "Scene-1";
				    Application.LoadLevel("Load");
					break;
				}
			}
		}
	}
}
